/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-10-12 16:45:32
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-10-30 15:14:15
 * @FilePath: /GateServer/src/entity/user.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "user.h"
#include <jsoncpp/json/value.h>
#include <regex>
#include "regex"
#include "../util/logger.h"
namespace Yizhi {
    User::User(const Json::Value &data){
        m_username=data["username"].asString();
        m_email=data["email"].asString();
        m_password=data["password"].asString();
        m_repsw=data["repsw"].asString();
        m_varify_code=data["varify_code"].asString();
    }

    bool User::validate()const{
        if(m_username.empty()||m_email.empty()||m_password.empty()||m_repsw.empty()){
            return false;
        }

        std::regex email_pattern(R"((\w+)(\.|_)?(\w*)@(\w+)(\.(\w+))+)");
        if(!std::regex_match(m_email,email_pattern)){
            return false;
        }
        return m_password==m_repsw;
    }

    bool User::validatewithoutpsw() const{
        if(m_username.empty()||m_email.empty()){
            return false;
        }

        std::regex email_pattern(R"((\w+)(\.|_)?(\w*)@(\w+)(\.(\w+))+)");
        if(!std::regex_match(m_email,email_pattern)){
            return false;
        }
        return true;
    }
    
    bool User::validatewithoutusername() const{
        if(m_email.empty()||m_password.empty()){
            return false;
        }
        LOG_INFO(m_password);

        std::regex email_pattern(R"((\w+)(\.|_)?(\w*)@(\w+)(\.(\w+))+)");
        if(!std::regex_match(m_email,email_pattern)){
            return false;
        }
        return true;
    }
    std::string User::toJsonstring()const{
        Json::Value root;
        root["username"]=m_username;
        root["email"]=m_email;
        root["password"]=m_password;
        root["repsw"]=m_repsw;
        root["varify_code"]=m_varify_code;
        return root.asString();
    }

    bool User::validatewithoutemail()const{
         if(m_username.empty()||m_password.empty()){
            return false;
    }       
        return true;
    }


    void User::serializeFields(Json::Value& json) const {
        json["id"] = m_uid;
        json["username"] = m_username;
        json["email"] = m_email;
    }

    
    Json::Value User::toJson() const {
        Json::Value json;
        serializeFields(json);
        return json;
    }
}